//
//  LYStatusOriginalView.swift
//  微博
//
//  Created by liuerying on 16/5/24.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit
import SnapKit
import SVProgressHUD

import SDWebImage
let StatusCellMargin:CGFloat = 8

//定义原创微博正文的字号
let orginalContentLabelFontSize :CGFloat = 14


class LYStatusOriginalView: UIView {

    var statutesModel: LYStatusViewModel? {
        
        didSet{
          
            headView.sd_setImageWithURL(statutesModel?.headViewURL)
          nameLabel.text = statutesModel?.status?.user?.name
            timeLabel.text = statutesModel?.time
            sourceLabel.text = statutesModel?.sourceText
         //微博来源 需要数据处理
            contentLabel.attributedText = statutesModel?.originalAttrbuiteString
                
                
            verifiedView.image = statutesModel?.verifiedView
            mbrankView.image = statutesModel?.mbrankView
            

            //给配图设置数据源
            picturesView.pictures = statutesModel?.status?.pic_urls
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置颜色
//        backgroundColor = UIColor.purpleColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI界面
   private func setupUI(){
    
    //测试颜色
    sepView.backgroundColor = UIColor.redColor()

    addSubview(sepView)
    
    addSubview(headView)
    addSubview(verifiedView)
    addSubview(nameLabel)
    addSubview(timeLabel)
    addSubview(sourceLabel)
    addSubview(contentLabel)
    addSubview(mbrankView)
    addSubview(picturesView)
    
    
    sepView.snp_makeConstraints { (make) in
        make.top.left.right.equalTo(self)
        make.height.equalTo(StatusCellMargin)
    }
    
    headView.snp_makeConstraints { (make) in
        make.left.equalTo(self).offset(StatusCellMargin)
      
        make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
        //设置高度
        make.width.height.equalTo(40)
        
    }
    
    verifiedView.snp_makeConstraints { (make) in
        make.bottom.right.equalTo(headView)
    }
    
    nameLabel.snp_makeConstraints { (make) in
        make.top.equalTo(headView.snp_top)
        make.left.equalTo(headView.snp_right).offset(StatusCellMargin)
    }
    
    mbrankView.snp_makeConstraints { (make) in
        make.top.equalTo(headView.snp_top)
        make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
    }
    
    timeLabel.snp_makeConstraints { (make) in
        make.bottom.equalTo(headView.snp_bottom)
        make.left.equalTo(headView.snp_right).offset(StatusCellMargin)
    }
    
    sourceLabel.snp_makeConstraints { (make) in
        make.bottom.equalTo(headView.snp_bottom)
        make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
    }
    
    contentLabel.snp_makeConstraints { (make) in
        make.top.equalTo(headView.snp_bottom).offset(StatusCellMargin)
        make.left.equalTo(headView.snp_left)
    }
    
    picturesView.snp_makeConstraints { (make) in
        make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
        make.left.equalTo(contentLabel.snp_left)
       
        
        //设置测试大小
        make.size.equalTo(CGSize(width: 100, height: 100))
        
    }
    
    //给原创视图添加约束
    self.snp_makeConstraints { (make) in
        make.bottom.equalTo(picturesView.snp_bottom).offset(StatusCellMargin)
    }
    
    
    //实现yylabel高亮状态下的点击事件的回调
    //(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect);
    contentLabel.highlightTapAction = { (containerView: UIView,text: NSAttributedString,range: NSRange,rect: CGRect) -> () in
        //            print(containerView)
        //            print(text)
        //            print(range)
        //            print(rect)
        
        //通过回调的属性字符串 和range 来截取用户点击的文本
        let str = text.string
        let subStr = (str as NSString).substringWithRange(range)
        print(subStr)
        SVProgressHUD.showInfoWithStatus(subStr)
        //使用遍历响应者链条 获取导航视图控制器
        if subStr.containsString("http") {
            //跳转到web页面
            let web = LYWebViewController()
            web.urlString = subStr
            self.findNavController()?.pushViewController(web, animated: true)
        }
    }

    
    
    
    }
    
    //分隔条
    private lazy var sepView:UIView = UIView()
    //头像
    lazy var headView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
   
    //用户认证
    lazy var verifiedView : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
   //用户等级
    lazy var mbrankView : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //用户名字
    lazy var nameLabel: UILabel = UILabel(title: "张沫凡", font: 15, textcolor: UIColor.darkGrayColor())
    //来源
    lazy var sourceLabel: UILabel = UILabel(title: "空间", font: 13, textcolor: UIColor.darkGrayColor())
    //时间
    lazy var timeLabel :UILabel = UILabel(title: "2016", font: 13, textcolor: UIColor.orangeColor())
    //内容
    lazy var contentLabel : YYLabel = {
        
        let yy = YYLabel()
        //设置最大的布局宽度
        yy.preferredMaxLayoutWidth = ScreeWidth - 2 * StatusCellMargin
        
        //设置自动换行
        yy.numberOfLines = 0
        
        
        return yy
        }()
        
       
    
    //图片视图
    lazy var picturesView :LYStatusPictureView = LYStatusPictureView()
    
}
